package com.api.league.controller;

import com.api.core.annotation.PowerEnable;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.league.form.LeagueTransferForm;
import com.api.league.model.LeagueTeam;
import com.api.league.service.LeagueTeamService;
import com.api.league.service.LeagueTransferService;
import com.api.league.vo.LeagueTeamMembersVo;
import com.api.league.vo.LeagueTeamVo;
import com.api.league.vo.LeagueTransferRecordVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author RenQiang
 * @date 2021/5/28
 */
@PowerEnable(name = "联赛战队", url = "/league/team")
@Api(value = "联赛战队", tags = {LeagueTeamController.API_TAG})
@RestController
@RequestMapping("/league/team")
@Validated
public class LeagueTeamController extends Ctrl {
    public static final String API_TAG = "联赛战队";

    private final LeagueTeamService leagueTeamService;
    private final LeagueTransferService leagueTransferService;

    public LeagueTeamController(LeagueTeamService leagueTeamService, LeagueTransferService leagueTransferService) {
        this.leagueTeamService = leagueTeamService;
        this.leagueTransferService = leagueTransferService;
    }

    @ApiOperation(value = "批量添加联赛战队", tags = {API_TAG}, notes = "批量添加联赛战队")
    @PostMapping("/add-batch")
    public Result batchAdd(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                           @ApiParam(value = "战队ID列表") @RequestParam Set<Long> teamIds) {
        List<LeagueTeam> leagueTeams = teamIds.stream()
                .map(teamId -> {
                    LeagueTeam leagueTeam = new LeagueTeam();
                    leagueTeam.setLeagueId(leagueId);
                    leagueTeam.setTeamId(teamId);
                    return leagueTeam;
                })
                .collect(Collectors.toList());
        leagueTeamService.save(leagueTeams);
        return ResultGenerator.genCreatedSuccessResult();
    }

    @ApiOperation(value = "批量删除联赛战队", tags = {API_TAG}, notes = "批量删除联赛战队")
    @PostMapping("/delete-batch")
    public Result batchDelete(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                              @ApiParam(value = "战队ID列表") @RequestParam Set<Long> teamIds) {
        leagueTeamService.deleteByTeamIds(leagueId, teamIds);
        return ResultGenerator.genDeleteSuccessResult();
    }

    @ApiOperation(value = "分页获取联赛战队列表", tags = {API_TAG}, notes = "分页获取联赛战队列表")
    @GetMapping("/find-by-page")
    public Result findByPage(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                             @ApiParam(value = "战队名称") @RequestParam(required = false) String teamName,
                             @ApiParam(value = "页号") @RequestParam(defaultValue = "1") Integer page,
                             @ApiParam(value = "分页大小") @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("leagueId", leagueId);
        if (!StringUtils.isEmpty(teamName)) {
            params.put("teamName", teamName);
        }
        PageInfo<LeagueTeamVo> pageInfo = leagueTeamService.findByPage(params, page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "批量保存队员转会记录", tags = {API_TAG}, notes = "批量保存队员转会记录")
    @PostMapping(value = "/create-transfer-record-batch")
    public Result batchCreateTransferRecords(@Valid @RequestBody @NotEmpty List<LeagueTransferForm> forms) {
        leagueTransferService.batchCreateTransferRecords(forms);
        return ResultGenerator.genCreatedSuccessResult();
    }

    @ApiOperation(value = "批量删除队员转会记录", tags = {API_TAG}, notes = "批量删除队员转会记录")
    @PostMapping(value = "/delete-transfer-record-batch")
    public Result batchDeleteTransferRecords(@ApiParam(value = "转会记录ID列表") @NotEmpty @RequestParam Set<Long> transferRecordIds) {
        leagueTransferService.deleteByIds(transferRecordIds.stream().map(Objects::toString).collect(Collectors.joining(",")));
        return ResultGenerator.genDeleteSuccessResult();
    }

    @ApiOperation(value = "获取队员转会记录", tags = {API_TAG}, notes = "获取队员转会记录")
    @GetMapping("/transfer-record-page")
    public Result transferList(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                               @ApiParam(value = "页号") @RequestParam(defaultValue = "1") Integer page,
                               @ApiParam(value = "分页大小") @RequestParam(defaultValue = "10") Integer size) {
        PageInfo<LeagueTransferRecordVo> pageInfo = leagueTransferService.findByPage(leagueId, page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "获取联赛所有队伍", tags = {API_TAG}, notes = "获取联赛所有队伍")
    @GetMapping("/detail-list")
    public Result detailList(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                             @ApiParam(value = "战队名称") @RequestParam(required = false) String teamName) {
        Map<String, Object> conditions = new HashMap<>(4);
        if (!StringUtils.isEmpty(teamName)) {
            conditions.put("teamName", teamName);
        }
        List<LeagueTeamMembersVo> teams = leagueTeamService.listDetail(leagueId, conditions);
        return ResultGenerator.genSuccessResult(teams);
    }
}
